package com.pr.makfilm.user.pages.event.reservation;

import com.google.common.collect.Table;
import com.pr.makfilm.core.cmp.authorization.UserAuthorities;
import com.pr.makfilm.core.cmp.table.AjaxDataTable;
import com.pr.makfilm.core.cmp.table.DataProvider;
import com.pr.makfilm.domain.ContactPerson;
import com.pr.makfilm.domain.Event;
import com.pr.makfilm.domain.Reservation;
import com.pr.makfilm.domain.School;
import com.pr.makfilm.domain.filter.OpenSearchDescription;
import com.pr.makfilm.domain.filter.ReservationFilter;
import com.pr.makfilm.service.IEventService;
import com.pr.makfilm.service.IReservationService;
import com.pr.makfilm.service.IRoomPlaceService;
import com.pr.makfilm.user.api.UserApiKeys;
import com.pr.makfilm.user.api.UserApiMappings;
import com.pr.makfilm.user.pages.AbstractUserPage;
import com.pr.makfilm.user.pages.event.reservation.cmd.ReservationWindow;
import com.pr.makfilm.user.pages.room.list.RoomListPage;
import com.pr.makfilm.user.utils.RoomPlaceUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.wicket.RestartResponseAtInterceptPageException;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.joda.time.DateTime;
import org.wicketstuff.annotation.mount.MountPath;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by p.radomski on 27.02.14.
 */
@MountPath(UserApiMappings.EVENT_RESERVATION_PAGE)
@AuthorizeInstantiation(UserAuthorities.USER)
public class EventReservationPage extends AbstractUserPage
{
   @SpringBean
   private IEventService eventService;

   @SpringBean
   private IReservationService reservationService;

   @SpringBean
   private IRoomPlaceService roomPlaceService;

   private AjaxDataTable table;

   private Table placeTable;

   public EventReservationPage(PageParameters parameters)
   {
      super(parameters);
      retrieveEvent(parameters);

   }

   @Override
   protected void onInitialize()
   {
      super.onInitialize();
      add(new Label("title", new PropertyModel<String>(getDefaultModel(), Event.FIELD_NAME)));
      add(new Label("date-time", new PropertyModel<DateTime>(getDefaultModel(), Event.FIELD_DATE_TIME)));
      List<IColumn<Reservation, String>> columns = new ArrayList<IColumn<Reservation, String>>();
      columns.add(new PropertyColumn<Reservation, String>(
              new ResourceModel("school"),
              Reservation.FIELD_SCHOOL + "." + School.FIELD_NAME,
              Reservation.FIELD_SCHOOL + "." + School.FIELD_NAME
      ));
      columns.add(new PropertyColumn<Reservation, String>(
              new ResourceModel("first.name"),
              Reservation.FIELD_CONTACT_PERSON + "." + ContactPerson.FIELD_FIRST_NAME,
              Reservation.FIELD_CONTACT_PERSON + "." + ContactPerson.FIELD_FIRST_NAME
      ));
      columns.add(new PropertyColumn<Reservation, String>(
              new ResourceModel("last.name"),
              Reservation.FIELD_CONTACT_PERSON + "." + ContactPerson.FIELD_LAST_NAME,
              Reservation.FIELD_CONTACT_PERSON + "." + ContactPerson.FIELD_LAST_NAME
      ));
      columns.add(new PropertyColumn<Reservation, String>(
              new ResourceModel("paid.place.amount"),
              Reservation.FIELD_PAID_PLACE_AMOUNT,
              Reservation.FIELD_PAID_PLACE_AMOUNT
      ));
      columns.add(new PropertyColumn<Reservation, String>(
              new ResourceModel("free.place.amount"),
              Reservation.FIELD_FREE_PLACE_AMOUNT,
              Reservation.FIELD_FREE_PLACE_AMOUNT
      ));
      columns.add(new PropertyColumn<Reservation, String>(
              new ResourceModel("phone"),
              Reservation.FIELD_CONTACT_PERSON + "." + ContactPerson.FIELD_PHONE,
              Reservation.FIELD_CONTACT_PERSON + "." + ContactPerson.FIELD_PHONE
      ));
      OpenSearchDescription<Reservation> osd = new OpenSearchDescription<Reservation>();
      ReservationFilter filter = new ReservationFilter();
      osd.setFilter(filter);
      DataProvider dataProvider = new DataProvider(reservationService, osd);
      table = new AjaxDataTable<Reservation>("table", columns, dataProvider);
      add(table);
      final ReservationWindow window = new ReservationWindow("window", getDefaultModel(), Model.of("dupa"));
      add(window);
      add(new AjaxLink<Void>("add")
      {
         @Override
         public void onClick(AjaxRequestTarget target)
         {
            window.show(target);
         }
      });
   }

   private void retrieveEvent(PageParameters parameters)
   {
      String sid = parameters.get(UserApiKeys.SID_KEY).toString();
      if (StringUtils.isNotBlank(sid))
      {
         Event obj = eventService.getBySid(sid);
         if (obj == null)
         {
            throw new RestartResponseAtInterceptPageException(RoomListPage.class);
         }
         placeTable = RoomPlaceUtils.convertToTable(roomPlaceService.getByRoom(obj.getRoom()));
         setDefaultModel(Model.of(obj));
      }
      else
      {
         throw new RestartResponseAtInterceptPageException(RoomListPage.class);
      }
   }
}
